#!/bin/bash
set -e
# yum install apr-devel openssl-devel java java-devel -y
cd /root/
wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.51/bin/apache-tomcat-8.5.51.tar.gz
tar -xf apache-tomcat-8.5.51.tar.gz
mv apache-tomcat-8.5.51 /usr/local/tomcat
useradd tomcat -s /sbin/nologin -M
cd /usr/local/tomcat
rm -rf logs
mkdir /var/log/tomcat
chown tomcat:tomcat /var/log/tomcat -R
ln -s /var/log/tomcat /usr/local/tomcat/logs
mv conf /etc/tomcat
ln -s /etc/tomcat ./conf
rm -rf /usr/local/tomcat/webapps/*
cat <<EOF > /etc/tomcat/tomcat.conf

# This variable is used to figure out if config is loaded or not.
TOMCAT_CFG_LOADED="1"

# In new-style instances, if CATALINA_BASE isn't specified, it will
# be constructed by joining TOMCATS_BASE and NAME.
TOMCATS_BASE="/usr/local/tomcat/"

# Where your java installation lives
JAVA_HOME="/usr/lib/jvm/jre"

# Where your tomcat installation lives
CATALINA_HOME="/usr/local/tomcat"

# System-wide tmp
CATALINA_TMPDIR="/usr/local/tomcat/temp"

# You can pass some parameters to java here if you wish to
#JAVA_OPTS="-Xminf0.1 -Xmaxf0.3"

# Use JAVA_OPTS to set java.library.path for libtcnative.so
# JAVA_OPTS="-Djava.library.path=/usr/lib"
JAVA_OPTS="-server -Xms2G -Xmx2G -XX:+AggressiveOpts -XX:+DisableExplicitGC"
# You can change your tomcat locale here
#LANG="en_US"

# Run tomcat under the Java Security Manager
SECURITY_MANAGER="false"

# Time to wait in seconds, before killing process
# TODO(stingray): does nothing, fix.
# SHUTDOWN_WAIT="30"

# If you wish to further customize your tomcat environment,
# put your own definitions here
# (i.e. LD_LIBRARY_PATH for some jdbc drivers)
EOF
cat <<EOF > /usr/lib/systemd/system/tomcat.service
[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target
[Service]
Type=simple
EnvironmentFile=/etc/tomcat/tomcat.conf
Environment="NAME="
ExecStart=/usr/local/tomcat/bin/catalina.sh run
ExecStop=/usr/local/tomcat/bin/catalina.sh stop
SuccessExitStatus=143
User=tomcat

[Install]
WantedBy=multi-user.target
EOF
rm -rf /etc/tomcat/server.xml
cat <<EOF > /etc/tomcat/server.xml
<?xml version="1.0" encoding="UTF-8"?>
<Server port="8005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
  <GlobalNamingResources>
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>

  <Service name="Catalina">

    <Connector port="8080" protocol="HTTP/1.1"
        URIEncoding="UTF-8"
               connectionTimeout="20000"
               redirectPort="8443" />
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
    <Engine name="Catalina" defaultHost="localhost">
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>
  </Service>
</Server>
EOF

cat <<'EOF' > /usr/local/tomcat/bin/setenv.sh
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$CATALINA_HOME/lib
export LD_LIBRARY_PATH
EOF
echo ""
cat /usr/local/tomcat/bin/setenv.sh
echo ""
set -x
export CATALINA_HOME=/usr/local/tomcat
cd $CATALINA_HOME/bin
tar -xf tomcat-native.tar.gz
cd tomcat-native-1.2.23-src/native/
./configure --with-java-home=/usr/lib/jvm/java-openjdk --prefix=/usr/local/tomcat
make && make install
set +x
chown tomcat:tomcat /etc/tomcat -R
chown tomcat:tomcat /usr/local/tomcat -R
systemctl daemon-reload
systemctl enable tomcat
systemctl start tomcat
systemctl status tomcat
set +e
